package com.cooper.service.admin;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooper.constants.DictConstants;
import com.cooper.constants.ResourceConstants;
import com.cooper.dao.admin.LocationDAO;
import com.cooper.domain.DataContainer;
import com.cooper.entity.LocationEntity;
import com.cooper.resource.IResourceManager;
import com.cooper.util.BeanUtils;
import com.cooper.vo.LocationVo;
import com.google.common.collect.Lists;

@Service
public class LocationService {
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public static final long ROOT_LOCATION_ID = 0L;
	
	@Autowired
	private IResourceManager resourceManager;
	
	@Autowired
	private LocationDAO locationDao;
	
	public LocationEntity loadById(long locationId) throws Exception{
		return (LocationEntity)this.resourceManager.getResItem(DictConstants.LOCATION, String.valueOf(locationId));
	}
	
	@SuppressWarnings("unchecked")
	public List<DataContainer> getChildLocationList(Long locationId, String lang){
		List<DataContainer> retList = Lists.newArrayList();
		List<LocationEntity> entityList = (List<LocationEntity>)this.resourceManager.getResListByGroup(
				DictConstants.LOCATION, "parent_id", String.valueOf(locationId));
		if(null != entityList){
			for(LocationEntity entity: entityList){
				DataContainer entry = new DataContainer();
				entry.setId(entity.getId());
				if(ResourceConstants.Lang.ZH_CN.equals(lang)){
					entry.setName(entity.getZhCn());
				} else {
					entry.setName(entity.getEnUs());
				}
				retList.add(entry);
			}
		}
		return retList;
	}
	
	/**
	 *  获取所有国家列表
	 * @return
	 */
	public List<DataContainer> getAllCountryList(String lang) {
		List<DataContainer> retList = Lists.newArrayList();
		List<DataContainer> continentList = this.getChildLocationList(ROOT_LOCATION_ID, lang);
		for(DataContainer continent: continentList){
			List<DataContainer> countryList = this.getChildLocationList(continent.getId(), lang);
			retList.addAll(countryList);
		}
		Collections.sort(retList, new Comparator<DataContainer>(){
			@Override
			public int compare(DataContainer o1, DataContainer o2) {
				return o1.getName().compareTo(o2.getName());
			}
		});
		return retList;
	}
	
	@SuppressWarnings("unchecked")
	public List<LocationEntity> getChildLocationEntities(Long locationId){
		return (List<LocationEntity>)this.resourceManager.getResListByGroup(
				DictConstants.LOCATION, "parent_id", String.valueOf(locationId));
	}
	
	public List<LocationVo> getAllLocationWithChilds(String lang){
		List<LocationVo> retList = Lists.newArrayList();
		//TODO
//		List<LocationEntity> entityList = this.getChildLocationEntities(ROOT_LOCATION_ID);
//		for(LocationEntity entity: entityList){
//			LocationVo locationVo = new LocationVo();
//			BeanUtils.copyProperties(entity, locationVo);
//			locationVo.setName(ResourceConstants.Lang.ZH_CN.equals(lang)?entity.getZhCn():entity.getEnUs());
//			retList.add(this.getLocationVoWithChilds(locationVo, lang));
//		}
		return retList;
	}
	
	private LocationVo getLocationVoWithChilds(LocationVo vo, String lang){
		if(ResourceConstants.Deleted.NO.equals(vo.getLeaf())){
			List<LocationEntity> childList = this.getChildLocationEntities(vo.getId());
			if(CollectionUtils.isNotEmpty(childList)){
				for(LocationEntity entity: childList){
					LocationVo childVo = new LocationVo();
					BeanUtils.copyProperties(entity, childVo);
					childVo.setName(ResourceConstants.Lang.ZH_CN.equals(lang)?entity.getZhCn():entity.getEnUs());
					childVo = this.getLocationVoWithChilds(childVo, lang);
					vo.addChild(childVo);
				}
			}
		}
		return vo;
	}
	
	public Long saveEntity(LocationEntity entity) throws Exception{
		return this.locationDao.saveEntity(entity);
	}

}
